<?php
class indexController extends Controller_Base
{
    public function init()
    {
        parent::noLogInJump();
    }
    
    public function indexAction()
    {
        $date = trim($this->getParam('date', ""), "\r\n\t ");
        $flg = intval($this->getParam('flg', 0));
        $modelname = trim(urldecode($this->getParam('modelname', "")), "\r\n\t ");
        $username = trim(urldecode($this->getParam('username', "")), "\r\n\t ");

        if($date == "" || !Model_Validator::timeYM($date)){
            $date = Model_Common::getFormatTimeYM();
        }
        
        try {
            $works = Model_Search::search($date, $flg, $modelname, $username);
        }catch (Exception $e){
            $this->errExit($e->getTraceAsString(), $e->getMessage());
        }
        
        $search['flg'] = $flg;
        $search['modelname'] = $modelname;
        $search['username'] = $username;
        
        $this->setData('search', $search);
        $this->setData('date', $date);
        $this->setData('works', $works);
        $this->setData('title', '首页');
        $this->setData('index', true);
        $this->setView('Work/search.tpl');
    }
    
    public function downloadAction()
    {
        if(Model_User::getLevelID() != 5){
            $this->notAuthWeb();        
            return;
        }
        
        $date = trim($this->getParam('date', ""), "\r\n\t ");
        $flg = intval($this->getParam('flg', 0));
        $modelname = trim(urldecode($this->getParam('modelname', "")), "\r\n\t ");
        $username = trim(urldecode($this->getParam('username', "")), "\r\n\t ");

        if($date == "" || !Model_Validator::timeYM($date)){
            $date = Model_Common::getFormatTimeYM();
        }
        
        try {
            $download = Model_Search::download($date, $flg, $modelname, $username);
        }catch (Exception $e){
            $this->errExit($e->getTraceAsString(), $e->getMessage());
        }
        
        Eren_DownLoad::xls("{$date}.xls", $date, $download);
    }
}